<template>
  <div id="all">
    <div class="tablse">
      <el-table
        :data="tableData"
        class="table"
        height="100%"
        ref="jdjgTable"
        @sort-change="sortChange"
        :header-cell-style="{
          padding: '0px',
          background: 'linear-gradient(0deg, #F4F6F9, #EAECF4, #F3F6F9)'
        }"
        :row-class-name="tabRowClassName"
        header-row-class-name="mapTable_th"
      >
        <el-table-column width="60" align="center" type="index" :index="addIndex" label="序号">
          <el-table-column width="60" align="center" type="index" label="检索"> </el-table-column>
        </el-table-column>
        <el-table-column label="结算单编号" align="center">
          <el-table-column align="center" sortable="custom" prop="settlementNo" width="160">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="供应商名称" align="center">
          <el-table-column align="center" sortable="custom" prop="providerName" width="180">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
            <template slot-scope="scope">
              <el-button type="text" class="xg czBtn" size="small" @click="checkCompony(scope.row)">
                {{ scope.row.providerName }}
              </el-button>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="结算单金额(万元)" align="center">
          <el-table-column align="center" sortable="custom" prop="settlementTotalPrice" width="150">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="调整后金额(万元)" align="center">
          <el-table-column align="center" sortable="custom" prop="afterAdjustPrice" width="150">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="状态" align="center">
          <el-table-column align="center" prop="stateDescription" width="180"> </el-table-column>
        </el-table-column>
        <el-table-column label="操作" align="center" width="260" fixed="right">
          <el-table-column align="center" width="260">
            <template slot-scope="scope">
              <div>
                <el-button
                  type="text"
                  class="xg czBtn"
                  @click="handelClick(scope.row, '发起付款')"
                  v-if="enterpriseType == 1"
                >
                  付款
                </el-button>
                <el-button type="text" class="xg czBtn" @click="handelClick(scope.row, '查看待支付结算单')">
                  查看
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <div class="pags">
      <element-pagination :Total="total" @getData="getPagChange"></element-pagination>
    </div>

    <el-dialog
      :width="width"
      :center="true"
      :title="title"
      v-if="settlementVisiable"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      :visible.sync="settlementVisiable"
    >
      <check-order-settlement
        v-if="title == '查看待支付结算单'"
        :settlementId="settlementId"
        :billing="true"
      ></check-order-settlement>
      <payment-modify
        v-if="title == '发起付款'"
        :payMoney="payMoney"
        :settlementId="settlementId"
        @close="close"
      ></payment-modify>
    </el-dialog>
  </div>
</template>

<script>
import PaymentModify from '../PaymentModify/index.vue'
import CheckOrderSettlement from '../CheckOrderSettlementModify/index.vue'
import ElementPagination from '../../../../components/ElementPagination.vue'
export default {
  props: {
    contractId: {
      type: [String, Number],
      require: true
    }
  },
  components: { ElementPagination, CheckOrderSettlement, PaymentModify },
  data() {
    return {
      enterpriseType: localStorage.getItem('enterpriseType'),
      width: '',
      title: '',
      payMoney: null,
      settlementVisiable: false,
      searchQrcoUsed: '',
      tableData: [],
      total: 0,
      size: 15,
      page: 1,
      settlementId: null,
      contractid: this.contractId
    }
  },
  watch: {
    contractId() {
      this.contractid = this.contractId
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    checkCompony(row) {
      const { providerId } = row
      if (providerId) {
        localStorage.setItem('supplierID', providerId)
        const { href } = this.$router.resolve({ name: 'SupplierDetail', query: { id: providerId } })

        window.open(href, '_blank')
      }
    },
    handelClick(scope, val) {
      this.width = '1200px'
      this.title = val
      this.settlementVisiable = true
      this.settlementId = scope.id
      if (val == '发起付款') {
        this.width = '486px'
        this.payMoney = scope.afterAdjustPrice
      }
    },
    close(val) {
      if (val) {
        if (val == '付款') {
          this.$emit('select', this.contractid)
        } else {
          this.getData()
        }
      }
      this.settlementId = null
      this.settlementVisiable = false
    },
    closeDialog(done) {
      this.settlementId = null
      done()
    },
    getDataChange() {},
    getPagChange(data) {
      this.page = data.currentPage
      this.size = data.pagesize
      this.getData()
    },
    getData() {
      if (!this.contractid) {
        return
      }
      this.$https({
        method: 'post',
        url: '/settlement/settlementList',
        data: {
          page: this.page,
          size: this.size,
          processState: 102,
          contractId: this.contractid
        }
      })
        .then(res => {
          this.tableData = res.data.records || []
          this.total = res.data.total || 0
        })
        .catch(err => {
          console.log(err)
        })
    },
    sortChange() {},
    addIndex(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    tabRowClassName({ row, rowIndex }) {
      let index = rowIndex + 1
      if (index % 2 == 0) {
        return 'warning-row'
      }
    }
  }
}
</script>
<style lang="less" scoped>
#all {
  height: calc(100vh - 304px);
  .tablse {
    height: calc(100% - 62px);
  }
}
</style>
